{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from faker import Faker\n",
    "from fake_useragent import UserAgent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1次生成：{'User-Agent': 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36'}\n",
      "第2次生成：{'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20130401 Firefox/31.0'}\n",
      "第3次生成：{'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36'}\n",
      "第4次生成：{'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1500.55 Safari/537.36'}\n",
      "第5次生成：{'User-Agent': 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36'}\n",
      "第6次生成：{'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36'}\n",
      "第7次生成：{'User-Agent': 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1467.0 Safari/537.36'}\n",
      "第8次生成：{'User-Agent': 'Mozilla/5.0 (X11; CrOS i686 4319.74.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36'}\n",
      "第9次生成：{'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0.6'}\n",
      "第10次生成：{'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36'}\n"
     ]
    }
   ],
   "source": [
    "# 使用 fake_useragent 库批量生成随机浏览器 UA\n",
    "ua = UserAgent()\n",
    "\n",
    "for i in range(10):\n",
    "    headers= {'User-Agent':ua.random}\n",
    "    print(f'第{i+1}次生成：{headers}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "存到本地 CSV 文件成功！！\n",
      "     姓名                         地址          手机号\n",
      "0   李兰英         安徽省燕县山亭杨路N座 724644  18605817389\n",
      "1    孔丹        湖南省长沙县高明萧路p座 284503  13141071857\n",
      "2   刘桂荣  广西壮族自治区马鞍山县南湖邯郸路t座 382234  14537578026\n",
      "3    陈波         湖北省欣县花溪余路F座 273967  13845320509\n",
      "4    苏洁        河北省凤兰市南溪贺路C座 305017  13039591081\n",
      "..  ...                        ...          ...\n",
      "95  林雪梅      青海省呼和浩特市孝南韩街E座 590786  18837339879\n",
      "96   苏东    澳门特别行政区南京市魏都董路F座 158740  13021444379\n",
      "97  程桂兰      山东省杭州市沈北新关岭路U座 368607  15781153363\n",
      "98   王莹    内蒙古自治区合山县海港海口街F座 838264  13081376660\n",
      "99  曹红霞       河南省惠州县蓟州重庆路A座 478706  18266782372\n",
      "\n",
      "[100 rows x 3 columns]\n"
     ]
    }
   ],
   "source": [
    "# 使用 faker 库批量生成随机数据\n",
    "# 文档地址：https://faker.readthedocs.io/en/stable/locales/zh_CN.html\n",
    "fake = Faker('zh_CN') # 使用简体中文\n",
    "\n",
    "def faker2csv(file_path, n):\n",
    "    datas = []\n",
    "    for i in range(n):\n",
    "        datas.append([fake.name(), fake.address(), fake.phone_number()])\n",
    "\n",
    "    # list转dataFrame\n",
    "    df = pd.DataFrame(data=datas, columns=['姓名', '地址', '手机号'])\n",
    "\n",
    "    # 保存到本地 CSV 文件\n",
    "    df.to_csv(file_path,index=False,encoding='utf-8')\n",
    "    print('存到本地 CSV 文件成功！！')\n",
    "    print(df)\n",
    "\n",
    "faker2csv('./fake_data.csv',100)    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
